//
//  CZWelcomeViewController.swift
//  项目框架
//
//  Created by yang on 16/2/29.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

import SDWebImage
class CZWelcomeViewController: UIViewController {

    
    override func loadView() {
        view = bgImageView
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        //view.backgroundColor = UIColor.whiteColor()
        
        setupUI()
}
    
    //当视图出现之后再调用方法开启动画
    override func viewDidAppear(animated: Bool) {
        
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    private func startAnimation(){
    
        //更新约束
        photoImageView.snp_updateConstraints { (make) -> Void in
            
            make.bottom.equalTo(-UIScreen.mainScreen().bounds.height + 100)
            
            //其他不需要修改
            make.centerX.equalTo(view.snp_centerX)
            
            make.size.equalTo(CGSizeMake(170/2, 170/2))
        }
        
        //动画的方法
        /*
            <#T##duration: NSTimeInterval##NSTimeInterval#> 动画时间
            usingSpringWithDamping 弹簧系数0-1，0
            initialSpringVelocity 初始加速度
        
        */
        UIView.animateWithDuration(2.0, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            
            //完成约束动画的时候,我们需要调用 -- 面试的时候,经常被问到的
            self.view.layoutIfNeeded()
            
            
            }) { (_) -> Void in
                
                print("完成之后切换界面")
                //发送通知切换视图
                NSNotificationCenter.defaultCenter().postNotificationName(kNotificaionChangeRootVC, object: nil)
        }
    
    }

    //私有方法
    private func  setupUI(){
    
    view.addSubview(photoImageView)
        view.addSubview(nameLabel)
        
        //添加约束 底部据view底部150， 中心X相同 宽高170、2
        photoImageView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(view.snp_bottom).offset(-150)
            
            make.centerX.equalTo(view.snp_centerX)
            
            make.size.equalTo(CGSizeMake(170/2, 170/2))
            
            let url = NSURL(string: CZUserViewModel.shareInstance.model?.profile_image_url ?? "")
            
            if let Ourl = url{
            
                //站位图片方法 placeholderImage
                photoImageView.sd_setImageWithURL(Ourl, placeholderImage: UIImage(named: "avatar_default_big"))
            }
        }
        
        nameLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(photoImageView.snp_bottom).offset(10)
            
            make.centerX.equalTo(view.snp_centerX)
        }
    
    }
    
    //懒加载
    private lazy var bgImageView:UIImageView = {
    
        let img = UIImageView()
        
        img.image = UIImage(named: "ad_background")
        
        return img
    }()
    
    //头像
    private lazy var photoImageView:UIImageView = {
    
        let img = UIImageView()
        
        img.image = UIImage(named: "avatar_default_big")
        
        //设置圆角
        img.layer.cornerRadius = 170 / 4
        
        //裁剪多余的部分
        img.layer.masksToBounds = true
        //边框颜色
        img.layer.borderColor = UIColor.grayColor().CGColor
        //边框宽度
        img.layer.borderWidth = 2
        
        return img
        
    }()
    //文字信息
    private lazy var nameLabel: UILabel = {
    
        let label = UILabel()
        
        label.textAlignment = .Center
        
        label.font = UIFont.systemFontOfSize(15)
        
        label.textColor = UIColor.darkGrayColor()
        
        // 添加用户名显示
        label.text = "\(CZUserViewModel.shareInstance.model?.screen_name ?? "")欢迎归来"
        
        return label
    }()

}
